<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Tpl
{
    private $_ci;

    public function __construct()
    {
        $this->_ci = & get_instance();
    }

    public function fetch($view, $data, $other_vars = false)
    {
        if ($other_vars) {
            $data = $data + $this->get_DataVar();
        }
        return $this->_ci->load->view($view, $data, true);
    }

    public function mail($view, $data)
    {
        return $this->_ci->load->view('..' . DIRECTORY_SEPARATOR . 'mail' . DIRECTORY_SEPARATOR . $view, $data, true);
    }

    public function render($layout = 'layouts/default', $default_helper = true, $return = false)
    {
        $data = $this->get_DataVar();

        if ($default_helper && empty($over_rideHelper)) {
            $allowed_helper = array(
                'header',
                'footer',
                'top_header'
            );
            foreach ($allowed_helper as $helper) {
                $data['get_' . $helper] = $this->fetch('common/' . $helper, $data);
            }
        }

        return $this->_ci->load->view($layout, $data, $return);
    }

    public function get_DataVar()
    {
        $data = array();
        foreach (get_object_vars($this) as $key => $val) {
            if ($key == '_ci') continue;
            $data [$key] = $val;
        }
        return $data;
    }
}